import { createRouter, createWebHashHistory} from 'vue-router'
import LayoutView from "@/views/LayoutView.vue";
import LoginView from "@/views/LoginView.vue";
import ProductCateView from '@/views/product/ProductCateView.vue';
import ProductListView from '@/views/product/ProductListView.vue';
import ProductOrderView from '@/views/product/ProductOrderView.vue';
import HomeView from '@/views/HomeView.vue';
import BannersCateView from '@/views/banners/BannersCateView.vue';
import BannersListView from '@/views/banners/BannersListView.vue';
import NoticeListView from '@/views/NoticeListView.vue';
import { useUserStore } from '@/stores/user';
import ArticleCateView from '@/views/article/ArticleCateView.vue';
import ArticleListView from '@/views/article/ArticleListView.vue';
import UserView from '@/views/UserView.vue';


const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      name: 'layout',
      component: LayoutView,
      // 路由重定向
      redirect: '/home',
      children: [
        {
          path: '/home',
          name: 'home',
          component: HomeView
        },
        {
          path: '/nolist',
          name: 'nolist',
          component: NoticeListView
        },
        {
          path: '/bancate',
          name: 'bancate',
          component: BannersCateView
        },
        {
          path: '/bancate',
          name: 'babcate',
          component: BannersCateView
        },
        {
          path: '/banlist',
          name: 'banlist',
          component: BannersListView
        },
        {
          path: '/procate',
          name: 'procate',
          component: ProductCateView
        },
        {
          path: '/prolist',
          name: 'prolist',
          component: ProductListView
        },
        {
          path: '/prorder',
          name: 'prorder',
          component: ProductOrderView
        },
        {
          path: '/artcate',
          name: 'aercate',
          component: ArticleCateView
        },
        {
          path: '/artlist',
          name: 'aerlist',
          component: ArticleListView
        },
        {
          path: '/userlist',
          name: 'userlist',
          component: UserView
        },
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView
    },
  ],
})


//登录访问拦截
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  if(!userStore.token && to.path != '/login'){
    next('/login')
  } else{
    next()
  }
  
})

export default router
